package api

import (
	"net/http"

	"example.com/rest/example/dao"
	"example.com/rest/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configMediaTypesRouter(router *httprouter.Router) {
	router.GET("/mediatypes", GetAllMediaTypes)
	router.POST("/mediatypes", AddMediaTypes)
	router.GET("/mediatypes/:argMediaTypeID", GetMediaTypes)
	router.PUT("/mediatypes/:argMediaTypeID", UpdateMediaTypes)
	router.DELETE("/mediatypes/:argMediaTypeID", DeleteMediaTypes)
}

func configGinMediaTypesRouter(router gin.IRoutes) {
	router.GET("/mediatypes", ConverHttprouterToGin(GetAllMediaTypes))
	router.POST("/mediatypes", ConverHttprouterToGin(AddMediaTypes))
	router.GET("/mediatypes/:argMediaTypeID", ConverHttprouterToGin(GetMediaTypes))
	router.PUT("/mediatypes/:argMediaTypeID", ConverHttprouterToGin(UpdateMediaTypes))
	router.DELETE("/mediatypes/:argMediaTypeID", ConverHttprouterToGin(DeleteMediaTypes))
}

// GetAllMediaTypes is a function to get a slice of record(s) from media_types table in the main database
// @Summary Get list of MediaTypes
// @Tags MediaTypes
// @Description GetAllMediaTypes is a handler to get a slice of record(s) from media_types table in the main database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.MediaTypes}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /mediatypes [get]
// http "http://localhost:8080/mediatypes?page=0&pagesize=20" X-Api-User:user123
func GetAllMediaTypes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "media_types", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllMediaTypes(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetMediaTypes is a function to get a single record from the media_types table in the main database
// @Summary Get record from table MediaTypes by  argMediaTypeID
// @Tags MediaTypes
// @ID argMediaTypeID
// @Description GetMediaTypes is a function to get a single record from the media_types table in the main database
// @Accept  json
// @Produce  json
// @Param  argMediaTypeID path int true "MediaTypeId"
// @Success 200 {object} model.MediaTypes
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /mediatypes/{argMediaTypeID} [get]
// http "http://localhost:8080/mediatypes/1" X-Api-User:user123
func GetMediaTypes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argMediaTypeID, err := parseInt32(ps, "argMediaTypeID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "media_types", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetMediaTypes(ctx, argMediaTypeID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddMediaTypes add to add a single record to media_types table in the main database
// @Summary Add an record to media_types table
// @Description add to add a single record to media_types table in the main database
// @Tags MediaTypes
// @Accept  json
// @Produce  json
// @Param MediaTypes body model.MediaTypes true "Add MediaTypes"
// @Success 200 {object} model.MediaTypes
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /mediatypes [post]
// echo '{"media_type_id": 50,"name": "ceYtKidugJmYclRkAqxitkwIn"}' | http POST "http://localhost:8080/mediatypes" X-Api-User:user123
func AddMediaTypes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	mediatypes := &model.MediaTypes{}

	if err := readJSON(r, mediatypes); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := mediatypes.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	mediatypes.Prepare()

	if err := mediatypes.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "media_types", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	mediatypes, _, err = dao.AddMediaTypes(ctx, mediatypes)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, mediatypes)
}

// UpdateMediaTypes Update a single record from media_types table in the main database
// @Summary Update an record in table media_types
// @Description Update a single record from media_types table in the main database
// @Tags MediaTypes
// @Accept  json
// @Produce  json
// @Param  argMediaTypeID path int true "MediaTypeId"
// @Param  MediaTypes body model.MediaTypes true "Update MediaTypes record"
// @Success 200 {object} model.MediaTypes
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /mediatypes/{argMediaTypeID} [put]
// echo '{"media_type_id": 50,"name": "ceYtKidugJmYclRkAqxitkwIn"}' | http PUT "http://localhost:8080/mediatypes/1"  X-Api-User:user123
func UpdateMediaTypes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argMediaTypeID, err := parseInt32(ps, "argMediaTypeID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	mediatypes := &model.MediaTypes{}
	if err := readJSON(r, mediatypes); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := mediatypes.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	mediatypes.Prepare()

	if err := mediatypes.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "media_types", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	mediatypes, _, err = dao.UpdateMediaTypes(ctx,
		argMediaTypeID,
		mediatypes)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, mediatypes)
}

// DeleteMediaTypes Delete a single record from media_types table in the main database
// @Summary Delete a record from media_types
// @Description Delete a single record from media_types table in the main database
// @Tags MediaTypes
// @Accept  json
// @Produce  json
// @Param  argMediaTypeID path int true "MediaTypeId"
// @Success 204 {object} model.MediaTypes
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /mediatypes/{argMediaTypeID} [delete]
// http DELETE "http://localhost:8080/mediatypes/1" X-Api-User:user123
func DeleteMediaTypes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argMediaTypeID, err := parseInt32(ps, "argMediaTypeID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "media_types", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteMediaTypes(ctx, argMediaTypeID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
